package im

import (
    //"fmt"
	//"errors"
	//"reflect"
	"encoding/json"
	"dhfshop/controllers/base"
	"thirdpaysdk/api/dahlpay"
	"github.com/astaxie/beego"
	"dhfshop/domain/im"
	"dhfshop/domain/pay"
	"dhfshop/service/redisgo"
)

type DahlPayNotifyController struct {
	base.BaseController
}

//dahlpay异步回调
func (this *DahlPayNotifyController) DahlPayNotify() {
	//notifyData := this.Ctx.Input.URL()
	//beego.Debug("DahlPayNotify notifyData:", string(notifyData))
	//
	var response dahlpay.DahlPayMsg
	response.FlowId = this.GetString("flowid")
	response.Paytime = this.GetString("paytime")
	response.Fee = this.GetString("fee")
	response.Ext = this.GetString("ext")
	response.Status = this.GetString("status")
	response.Sign = this.GetString("sign")
	body, err := json.Marshal(response)
	if err != nil {
		beego.Debug("DahlPayNotify:", err.Error())
	}
	beego.Debug("DahlPayNotify Marshal Data:", string(body))
	//
	var payInfoCache *paylayer.PayInfoCache = new(paylayer.PayInfoCache)
	cacheKey := "payorder_" + response.Ext
	redisconn := redisgo.GetInstance()
	if cacheValue, err := redisconn.GetString(cacheKey); err == nil && len(cacheValue) > 5  {
	    //beego.Debug("cache pay info:", cacheValue)
		if err = json.Unmarshal([]byte(cacheValue), payInfoCache); err == nil {
			
		}
	}else{
	    beego.Debug("cache pay info err:", err.Error())
		return 
	}
	reqMap, err := dahlpay.DahlPayNotify(&response, payInfoCache.MchId, payInfoCache.AppKey)
	if err != nil {
	    beego.Debug("DahlPayNotify:", err.Error())
		return
	}
	reqMap["LocalKind"] = "DAHL_PAY_NOTIFY"
	reqMap["attach"] = payInfoCache.OrderKind
	reqMap["currency"] = payInfoCache.Currency
	imlayer.PushMsgSvrIns.PushDao <- &reqMap
	this.Ctx.WriteString("success")
	return
}
